<?php if (!defined('THINK_PATH')) exit();?><!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="/Public/Admin/css/shortcut icon" href="favicon.ico">
	<link href="/Public/Admin/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
	<link href="/Public/Admin/css/animate.css" rel="stylesheet">
	<link href="/Public/Admin/css/style.css?v=4.1.0" rel="stylesheet">
	<link href="/Public/Admin/js/plugins/cityPicker/city-picker.css" rel="stylesheet">
	<link href="/Public/Admin/js/plugins/sweetalert/sweetalert.css" rel="stylesheet">
	<link href="/Public/Admin/css/edit.css" rel="stylesheet"/>

	<link href="/Public/Admin/css/selector/zTreeStyle/zTreeStyle.css" rel="stylesheet">
	<link href="/Public/Admin/js/plugins/BusinessSelector/BusinessSelector.css" rel="stylesheet"/>
	<link href="/Public/Admin/css/selector/selectorstyle.css" rel="stylesheet"/>
	<style>
		.form-control{width:100%;}
	</style>
</head>
<body class="gray-bg">

<body class="gray-bg body_box">
<div class="wrapper wrapper-content animated fadeInRight">
	<div class="row">

		<form class="js-ajax-forms" id="signupForm" action="<?php echo U('Admin/Business/edit');?>" method="post" enctype="multipart/form-data">
			<div class="col-sm-6">

				<div class="ibox float-e-margins">

					<div class="ibox-content">
						<input type="hidden" name="ID" id="ID" value="<?php echo ($business["ID"]); ?>"/>

						<h2>业务</h2>

							<div class="form-group">
								<label for="Name">业务名*</label>
								<input id="Name" name="Name" value="<?php echo ($business["Name"]); ?>" class="form-control" type="text" aria-required="true" aria-invalid="true" class="error">
							</div>
							<div class="form-group">
								<label for="ParentName">父业务</label>
								<input id="ParentName" name="ParentName" value="<?php echo ($business["ParentName"]); ?>" class="form-control" type="text"/>
								<input id="ParentID" name="ParentID" value="<?php echo ($business["ParentID"]); ?>" type="hidden"/>
							</div>
						<div class="form-group" >
							<label for="DepName">主办部门</label>
							<input id='DepID' name='DepID' value="<?php echo ($business["DepID"]); ?>" type="hidden">
							<input id="DepName" name="DepName" value="<?php echo ($business["DepName"]); ?>" class="form-control selector-icon"  onclick="openselector('DepID','DepName')" data-toggle="modal" data-target="#myModal" type="text">
						</div>
						<div class="form-group" >
							<label for="UserName">主办人</label>
							<input id='UserID' name='UserID' value="<?php echo ($business["UserID"]); ?>" type="hidden">
							<input id="UserName" name="UserName" value="<?php echo ($business["UserName"]); ?>" class="form-control selector-icon"  onclick="openselector('UserID','UserName')" data-toggle="modal" data-target="#myModal" type="text">
						</div>
							<div class="form-group">
								<label for="Memo">说明</label>
								<textarea id="Memo" name="Memo" class="form-control diff-textarea" rows="4"><?php echo ($business["Memo"]); ?></textarea>
							</div>


						<div class="form-group row">
							<div class="col-sm-8 col-sm-offset-3">
								<button class="btn btn-primary" type="submit">保存</button>
								<a class="btn btn-white" href="javascript:void(0);" onclick="self.location=document.referrer;">返回</a>
							</div>
						</div>

					</div>
				</div>
			</div>

				<div class="col-sm-6">
					<div class="ibox float-e-margins">

						<div class="ibox-content">
							<h2>附件</h2>
							<div class="form-group row">
								<table id="AttachList" class="table table-bordered">
									<tr><th>材料目录名</th><th>说明</th><th>操作</th></tr>
									<?php if(is_array($attachList)): foreach($attachList as $key=>$vo): ?><tr>
											<td>
												<input value="<?php echo ($vo["Name"]); ?>" readonly="readonly" name="AttachName" class="form-control attachName" type="text" aria-required="true" aria-invalid="true" class="error">
											</td>
											<td>
												<textarea name="AttachMemo" readonly="readonly" class="form-control diff-textarea attachMemo" rows="1"><?php echo ($vo["Memo"]); ?></textarea>
											</td>
											<td>
												<button data-id="<?php echo ($vo["ID"]); ?>" class="btn btn-default del" type="button">删除</button>
											</td>
										</tr><?php endforeach; endif; ?>

									<tr>
										<td>
											<input id="AttachName" name="AttachName" class="form-control" type="text" aria-required="true" aria-invalid="true" class="error">
										</td>
										<td>
											<textarea id="AttachMemo" name="AttachMemo" class="form-control diff-textarea" rows="1"></textarea>
										</td>
										<td>
											<button id="attach-add" class="btn btn-success" type="button">添加</button>
										</td>
									</tr>
								</table>
							</div>
							</div>
						</div>
				</div>

		</form>
	</div>
</div>

<script src="/Public/Admin/js/jquery.min.js?v=2.1.4"></script>
<script src="/Public/Admin/js/bootstrap.min.js?v=3.3.6"></script>
<script src="/Public/Admin/js/plugins/layer/layer.min.js"></script>
<script src="/Public/Admin/js/plugins/sweetalert/sweetalert.min.js"></script>
<script type="text/javascript" src="/Public/Admin/js/ajaxForm.js"></script>
<script src="/Public/Admin/js/plugins/validate/jquery.validate.min.js"></script>
<script src="/Public/Admin/js/plugins/validate/messages_zh.min.js"></script>
<script src="/Public/Admin/js/plugins/cityPicker/city-picker.data.js"></script>
<script src="/Public/Admin/js/plugins/cityPicker/city-picker.js"></script>
<script src="/Public/Admin/js/plugins/cityPicker/main.js"></script>
<script src="/Public/Admin/js/plugins/layer/laydate/laydate.js"></script>
<script type="text/javascript" src="/Public/Admin/js/plugins/BusinessSelector/jquery.ztree.all.js"></script>
<script type="text/javascript" src="/Public/Admin/js/plugins/BusinessSelector/BusinessSelector.js"></script>
<script src="/Public/Admin/js/plugins/selector/selector.js"></script>
<script type="text/javascript">
	$(document).ready(function () {

		var businessSelector = new BusinessSelector({
			element : '#ParentName',
			title : '选择父业务',
			multiple : false,
			button : [{
				title:'确定',
				onclick:function(data){
					$('#ParentID').val(data.id);
					$('#ParentName').val(data.name);
				}
			},{
				title : '取消',
				onclick : 'close'
			}]
		});

		$('#attach-add').on('click',function(){
			var attachName = $.trim($('#AttachName').val());
			var attachMemo = $.trim($('#AttachMemo').val());
			if(attachName){
				var tr = '<tr class="new"><td><input readonly="readonly" class="form-control attachName" type="text" name="AttachName[]" value="'+attachName+'"/></td><td><input readonly="readonly" class="form-control attachMemo" type="text" name="AttachMemo[]" value="'+attachMemo+'"/></td><td><button type="button" class="btn btn-default del">删除</button></td></tr>';
				var lastTr = $('#AttachList').find('tr:last-child');
				$(tr).insertBefore(lastTr);
				$('#AttachName').val('');
				$('#AttachMemo').val('');
			}
		});
		$('#AttachList').on('click','button.del',function(){
			var _this = this;
				swal({
						title: "附件操作",
						text: "确认删除？",
						type: "info",
						showCancelButton: true,
						confirmButtonColor: "#DD6B55",
						confirmButtonText: "确认",
						cancelButtonText: "返回",
						closeOnConfirm: false,
						closeOnCancel: true
					},
					function (isConfirm) {
						if (isConfirm) {
							$.ajax("<?php echo U('Admin/Business/deleteFile');?>",{
								type : 'POST',
								dataType : 'json',
								data : {
									id : $(_this).data('id')
								},
								success : function(data){
									console.log(data);
									$(_this).closest('tr').remove();
									swal("删除成功！", "您已经删除了这条信息。", "success");
								},
								error : function(){
									swal("删除失败！", "错误", "error");
								}
							});

							//$(_this).closest('tr').remove();
							//document.location.reload();
						} else {
							//document.location.href = "<?php echo U('Admin/Business/index');?>";
							//swal("已取消", "您取消了删除操作！", "error");
							//return true;
						}
					});
		});

		$.validator.setDefaults({
			highlight: function (element) {
				$(element).closest('.form-group').removeClass('has-success').addClass('has-error');
			},
			success: function (element) {
				element.closest('.form-group').removeClass('has-error').addClass('has-success');
			},
			errorElement: "span",
			errorPlacement: function (error, element) {
				if (element.is(":radio") || element.is(":checkbox")) {
					error.appendTo(element.parent().parent().parent());
				} else {
					error.appendTo(element.parent());
				}
			},
			errorClass: "help-block m-b-none",
			validClass: "help-block m-b-none "
		});

		$.validator.addMethod("isPhone", function (value, element) {
			var phone = /^1[34578]\d{9}$/;
			return phone.test(value);
		}, "请输入正确的手机号码");

		var icon = "<i class='fa fa-times-circle'></i> ";
		var form = $('form.js-ajax-forms');
		var formloading = false;
		//表单验证开始
		form.validate({
			//验证规则
			rules: {
				Name: {
					required: true
				},
				Memo: {
					rangelength: [0, 255]//输入值必须介于 15 和 18 之间。
				}
			},
			//验证未通过提示消息
			messages: {
				Name: {
					required: icon + "请输入业务名",
				},
				Memo: {
					maxlength: icon + "业务说明过长"
				}
			},
			//验证通过，提交表单
			submitHandler: function (forms) {
				if (formloading)
					return;
				var attachList = '{';
				$('#AttachList').find('tr.new').each(function(){
					if($(this).find('.attachName').length > 0){
						var line = {Name:$(this).find('.attachName').val(),Memo:$(this).find('.attachMemo').val()};
						console.log(JSON.stringify(line));
						attachList += JSON.stringify(line);
					}
				});
				attachList += '}';

				$(forms).ajaxSubmit({
					url: form.attr('action'), //按钮上是否自定义提交地址(多按钮情况)
					data:{attachList:JSON.stringify(attachList)},
					dataType: 'json',
					beforeSubmit: function (arr, $form, options) {
						formloading = true;
					},
					success: function (data, statusText, xhr, $form) {
						formloading = false;
						if (data.status) {
							//添加成功
							swal({
										title: "继续编辑？",
										text: "操作成功！",
										type: "success",
										showCancelButton: true,
										confirmButtonColor: "#DD6B55",
										confirmButtonText: "确认",
										cancelButtonText: "返回",
										closeOnConfirm: false,
										closeOnCancel: false
									},
									function (isConfirm) {
										if (isConfirm) {
											//swal("删除成功！", "您已经永久删除了这条信息。", "success");
											document.location.reload();
										} else {
											document.location.href = "<?php echo U('Admin/Business/index');?>";
											//swal("已取消", "您取消了删除操作！", "error");
											return true;
										}
									});

						} else {
							swal("操作失败", data.info, "error");
						}
					}
				});
			}
		});
	});
</script>
</body>
</html>